package com.shuwen.gcdj.controller.plan;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuwen.gcdj.bean.DictResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.basicsEquitment.response.EquipmentResp;
import com.shuwen.gcdj.bean.plan.request.*;
import com.shuwen.gcdj.bean.plan.response.PlanDetailResp;
import com.shuwen.gcdj.bean.plan.response.SecondPlanTreeResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.service.plan.PlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;;
import java.util.List;

/**
 * @author :yys
 * 计划管理
 * @date : 2023/5/9 13:56
 */
@Api(tags = "基础运维 - 计划管理")
@RestController
@RequestMapping("/plancyclemanagement")
@Slf4j
public class PlanController extends AuthController {


    @Autowired
    private PlanService planService;

    @ApiOperation(value = "获取日历数据")
    @PostMapping(value = "/getsecondplandetaillist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SecondPlanTreeResp>> getThirdStructurePage(@RequestBody SecondPlanReq req) {
        List<SecondPlanTreeResp> sList = planService.getThirdStructurePage(req);
        return CommonResult.success(sList);
    }

    @ApiOperation(value = "生成计划明细")
    @PostMapping(value = "/createmaintainplandetail")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> createmaintainplandetail(HttpServletRequest request, @RequestBody CreateMaintainPlanReq req) throws Exception {
        VoidResp addResp = planService.createmaintainplandetail(initUser(request),req);
        return CommonResult.success(addResp);
    }

    @ApiOperation(value = "获取计划明细分页信息")
    @PostMapping(value = "/getplandetailpagelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<PlanDetailResp>> getplandetailpagelist(@RequestBody PlanDetailPageReq req) {
        Page<PlanDetailResp> planDetailRespPage = planService.getplandetailpagelist(req);
        return CommonResult.success(CommonPage.restPage(planDetailRespPage));
    }

    @ApiOperation(value = "一键修改日期")
    @PostMapping(value = "/editallsecondmaintainorderstatebydate")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> editallsecondmaintainorderstatebydate(HttpServletRequest request,@RequestBody EditallSecondReq req) {
        VoidResp voidResp = planService.editallsecondmaintainorderstatebydate(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "一键关闭")
    @PostMapping(value = "/closeallfirstmaintainorderstatebyday")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> closeallfirstmain(HttpServletRequest request,@RequestBody CloseAllFirstMainReq req) {
        VoidResp voidResp = planService.closeallfirstmain(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "编辑一级保养计划状态")
    @PostMapping(value = "/editfirstmaintainorderstate")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> editfirstmaintainorderstate(HttpServletRequest request, @RequestBody EditFirstItemReq req) {
        VoidResp voidResp = planService.editfirstmaintainorderstate(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "编辑二级保养计划日期")
    @PostMapping(value = "/editsecondmaintainorderstate")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> editsecondmaintainorderstate(HttpServletRequest request,@RequestBody EditSecondItemReq req) {
        VoidResp voidResp = planService.editsecondmaintainorderstate(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "获取上一次计划生成执行的结束日期")
    @GetMapping(value = "/getmaxexecdatebystrategyid/{equipmentId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<String> getmaxexecdatebystrategyid(@PathVariable String equipmentId) {
        if(StringUtils.isEmpty(equipmentId)){
            return CommonResult.failed("请选择先选择设备");
        }
        String s = planService.getmaxexecdatebystrategyid(equipmentId);
        return CommonResult.success(s);
    }

    @ApiOperation(value = "当日计划统计")
    @PostMapping(value = "/getCureDatePlan")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<SecondPlanTreeResp>> getCureDatePlan(@Valid @RequestBody PlanIndexReq req) {
        List<SecondPlanTreeResp> sList = planService.getCureDatePlan(req);
        return CommonResult.success(sList);
    }

    @ApiOperation(value = "获取设备信息")
    @GetMapping(value = "/getequipmentbyid/{equipmentId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<EquipmentResp> getequipmentbyid(@PathVariable("equipmentId") String equipmentId) {
        if(StringUtils.isEmpty(equipmentId) || equipmentId.equals("0")){
            return CommonResult.success(new EquipmentResp());
        }
        EquipmentResp equipmentResp = planService.getequipmentbyid(equipmentId);
        return CommonResult.success(equipmentResp);
    }

    @ApiOperation(value = "获取设备编号")
    @GetMapping(value = "/getstructuretypecode")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getstructuretypecode() {
        List<DictResp> dictRespList = planService.getstructuretypecode();
        return CommonResult.success(dictRespList);
    }

    @ApiOperation(value = "获取计划周期")
    @GetMapping(value = "/getstructuretypeqi")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getstructuretypeqi() {
        List<DictResp> list = planService.getstructuretypeqi();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取计划状态")
    @GetMapping(value = "/getplanstate")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getplanstate() {
        List<DictResp> list = planService.getplanstate();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "分派人员")
    @GetMapping(value = "/getplanper/{equipmentId}")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getplanper(@PathVariable("equipmentId") String equipmentId) {
        if(StringUtils.isEmpty(equipmentId) || equipmentId.equals("0")){
            return CommonResult.success(null);
        }
        List<DictResp> list = planService.getplanper(equipmentId);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "计划类型")
    @GetMapping(value = "/getplantype")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getplantype() {
        List<DictResp> list = planService.getplantype();
        return CommonResult.success(list);
    }


}
